﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.thorqq.magictimer.SettingActivityTimer.ActionButtonListener;
import com.thorqq.magictimer.SettingActivityTimer.ActionDeleteListener;
import com.thorqq.magictimer.SettingActivityTimer.ActionLongClickListener;
import com.thorqq.magictimer.animation.ExpandAnimation;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TAction;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class AdapterAction extends BaseAdapter
{

    public final class ViewHolder
    {        
        public TAction action;
        public TextView actionDetail;
        public Button deleteAction;
        public LinearLayout layoutActionDetail;
        public ImageView show;

        public ChildView childView = null;
        public ChildViewParent parent;
        public int visibility = View.GONE;
        public LinearLayout layoutDown;
        public ExpandAnimation expandAni;
    }

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    @SuppressWarnings("unused")
    private Context mContext;

    public AdapterAction(Context context)
    {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mData = new ArrayList<Map<String, Object>>();
    }

    public AdapterAction(Context context, List<Map<String, Object>> data)
    {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mData = data;
    }

    public void add(Map<String, Object> map)
    {
        mData.add(map);
    }

    public void removeItem(int position)
    {
        mData.remove(position);
    }

    public void updateItem(int position, String key, Object obj)
    {
        if (position < mData.size())
        {
            mData.get(position).put(key, obj);
        }
    }

    public int getCount()
    {
        return mData.size();
    }

    public Object getItem(int position)
    {
        if (position < mData.size())
        {
            return mData.get(position);
        }
        else
        {
            return null;
        }
    }

    public long getItemId(int position)
    {
        if (position < mData.size())
        {
            return position;
        }
        else
        {
            return -1;
        }
    }
    
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.action_item, null);
            holder.actionDetail = (TextView) convertView.findViewById(R.id.btnActionDetail);
            holder.deleteAction = (Button) convertView.findViewById(R.id.btnDeleteAction);
            holder.layoutActionDetail = (LinearLayout)convertView.findViewById(R.id.LinearLayoutActionDetail);
            holder.layoutDown        = (LinearLayout)convertView.findViewById(R.id.LayoutActionItemDown);
            holder.show = (ImageView)convertView.findViewById(R.id.ivActionShowMore);
                        
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.action = (TAction) mData.get(position).get("action");

        String actionName = holder.action.getName();
        String actionDesc = holder.action.getDescription();
        if(actionDesc != null)
        {
            actionName += "：" + actionDesc;
        }
        
        holder.actionDetail.setText(actionName);
        //长按
        ActionLongClickListener LongListener = (ActionLongClickListener)mData.get(position).get("longListener");
        LongListener.setTag(holder.action);
        holder.layoutActionDetail.setOnLongClickListener(LongListener);
        //删除
        ActionDeleteListener deleteListener = (ActionDeleteListener)mData.get(position).get("deleteListener");
        deleteListener.setTag(holder.action);
        holder.deleteAction.setOnClickListener(deleteListener);

        // 子布局
        setChildViewVisibility(holder, position);
        //弹出设置界面
        ActionButtonListener listener = (ActionButtonListener)mData.get(position).get("listener");
        listener.setTag(holder.action);
        holder.layoutActionDetail.setOnClickListener(listener);
        
        if(listener.willNewActivity())
        {
            holder.show.setVisibility(View.VISIBLE);
            holder.show.setImageResource(R.drawable.arrow_right);
        }

        return convertView;
    }
    
    private void setChildViewVisibility(ViewHolder holder, final int position)
    {
        // 子布局
        holder.visibility = (Integer) mData.get(position).get("visibility");
        holder.parent = (ChildViewParent) mData.get(position).get("parent");
        ChildView newView = (ChildView) mData.get(position).get("childView");
        //动作类型发生变更
        if(holder.childView != newView)
        {
            holder.layoutDown.setVisibility(View.GONE);
            holder.layoutDown.removeAllViews();
        }
        
        holder.childView = newView;
        if(holder.childView == null)
        {
            holder.show.setVisibility(View.GONE);
            return;
        }
        
        //停止动作
        holder.childView.setTag(holder.action);
        holder.show.setVisibility(View.VISIBLE);

        Integer integer = (Integer) mData.get(position).get("stop");
        if(integer != null && integer == 1)
        {
            holder.childView.onClose();
        }

        if(holder.visibility != holder.layoutDown.getVisibility())
        {
            if(holder.visibility == View.VISIBLE)
            {
                if(!holder.childView.isInitialize())
                {
                    // 子布局初始化
                    holder.childView.initLayout(holder.parent);
                    holder.childView.setParentScrollView((ScrollView)mData.get(position).get("scrollview"));
                    holder.layoutDown.addView(holder.childView.getLayoutView());
                    //必须要在 addView 之后才能 new ExpandAnimation
                    //每增加或者删除一个，就是调用new ViewHolder()
                    holder.expandAni = new ExpandAnimation(holder.layoutDown, ExpandAnimation.DEFAULT_DURATION);
                }
                else
                {
                    holder.childView.updateLayout();
                }
            }
            else
            {
                holder.childView.onClose();
            }
            
            holder.expandAni.reset();
            holder.expandAni.setVisibility(holder.visibility);
            holder.layoutDown.startAnimation(holder.expandAni);

        }
        else if(holder.visibility == holder.layoutDown.getVisibility()
                && holder.visibility == View.VISIBLE)
        {
            holder.childView.updateLayout();
        }
        
        //向上、向下的箭头
        if(holder.visibility == View.VISIBLE)
        {
            holder.show.setImageResource(R.drawable.arrow_up);
        }
        else
        {
            holder.show.setImageResource(R.drawable.arrow_down);
        }

    }
}
